<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增信息</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="x-body">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <input id='hiddenId' type='hidden' th:value='${clientProduct.id}' name='id'/>
                <div class='layui-form-item'>
                    <label for='titleName' class='layui-form-label'><span class='x-red'>*</span>产品名称</label>
                    <div class='layui-input-inline'>
                        <input th:value='${clientProduct.title}' class='layui-input' placeholder='请输入产品名称' type='text'
                               name='title' id='titleName' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'>图片</label>
                    <div class='layui-input-inline'>
                        <div class="layui-upload-drag" id="headImg">
                            <i class="layui-icon" style="font-size:50px;">&#xe67c;</i>
                            <p>点击上传，或将文件拖拽到此处</p>
                            <div class="layui-hide" id="editPhoto">
                                <hr>
                                <img th:src="@{${clientProduct.picture}}" id="headerImg" alt="图片">
                            </div>
                        </div>
                    </div>
                </div>
                <input type="hidden" id="picture" name="picture" th:value="${clientProduct.picture}">
                <div class='layui-form-item layui-form-text'>
                    <label for='content' class='layui-form-label'><span class='x-red'>*</span>内容描述</label>
                    <div class='layui-input-inline'>
                        <textarea th:text='${clientProduct.content}' class='layui-textarea' placeholder=产品描述
                                  name='content' id='content' lay-verify='required'></textarea>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label for='sort' class='layui-form-label'><span class='x-red'>*</span>排序</label>
                    <div class='layui-input-inline'>
                        <input th:value='${clientProduct.sort}' class='layui-input' type='number'
                               name='sort' id='sort' lay-verify='required'>
                    </div>
                </div>
                <div class='layui-form-item'>
                    <label class='layui-form-label'><span class='x-red'>*</span>状态</label>
                    <div class='layui-input-inline'>
                        <select id="selectType" name="status" lay-filter="status" th:value="${clientProduct.status}">
                            <option value="1">有效</option>
                            <option value="0">无效</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <button class="layui-btn layui-btn-danger" lay-filter="add" lay-submit="">
                        <i class="iconfont">&#xe6ad;</i>立即提交
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    layui.use(['form', 'layer', 'upload'], function () {
        $ = layui.jquery;
        var form = layui.form,
            upload = layui.upload,
            layer = layui.layer;
        var photo = $('#headerImg').attr('src');
        //console.log(photo);
        if (photo != 'undefined' && photo != null && photo != ''){
            $('#editPhoto').removeClass('layui-hide');
        }else {
            $('#editPhoto').addClass('layui-hide');
        }

        var uploadInst = upload.render({
            elem: '#headImg',
            url: '/client/fileUpload/upload',
            exts: 'jpg|png|jpeg|gif',
            size: 2000,
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#editPhoto').removeClass('layui-hide').find('img').attr('src', result);
                });
            },
            done: function (res) {
                if (res.code == 0) {
                    layer.msg("上传成功");
                    $("#picture").val(res.data.src);
                } else {
                    layer.msg(res.msg);
                }
            },
            error: function () {
                //上传失败，并实现重传
                layer.confirm('上传失败，您是否需要重新上传?', {icon: 3, title: '提示'}, function (index) {
                    layer.close(index);
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            $.ajax({
                url: "/client/product/saveOrUpdate",
                type: "POST",
                data: data.field,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 500 || result.code == 5000100 || result.code == 5000101 || result.code == 5000102) {
                        layer.alert(result.msg);
                    } else {
                        layer.alert("设置成功", {icon: 6}, function () {
                            xadmin.close();
                            xadmin.father_reload();
                        });
                    }
                }
            });
            return false;
        });
        initSelectType();
    });
</script>
</body>
</html>
